package com.atguigu.flink0922.chapter05.source;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.util.serialization.JSONKeyValueDeserializationSchema;

import java.util.Properties;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/3/2 11:31
 */
public class Flink03_Source_Kafka {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        
        final Properties props = new Properties();
        props.setProperty("bootstrap.servers", "hadoop162:9092,hadoop163:9092");
        props.setProperty("group.id", "Flink03_Source_Kafka");
        props.setProperty("auto.offset.reset", "latest");
        
        //        env
        //            .addSource(new FlinkKafkaConsumer<>("sensor", new SimpleStringSchema(), props))
        //            .print();
        
        final DataStreamSource<ObjectNode> result = env
            .addSource(new FlinkKafkaConsumer<>("sensor", new JSONKeyValueDeserializationSchema(true), props));
        result
            .map(new MapFunction<ObjectNode, String>() {
                @Override
                public String map(ObjectNode value) throws Exception {
                    return value.get("value").toString();
                }
            })
            .print();
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
